package com.hu.zhcc.shiro.controller.Reponsitory;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.Reponsitory.RIllness;
import com.hu.zhcc.shiro.service.Responsitory.RillnessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/***
 * 疾病库
 *
 */
@RestController
public class RillnessController {

    @Autowired
    private RillnessService rillnessService;

    /***
     * 分页及条件查询
     * @param offset
     * @param limit
     * @param illName
     * @return
     */
    @GetMapping("illAll")
    private Page<RIllness> illAll(String offset, String limit, String illName) {
        RIllness rIllness = new RIllness(illName);
        Map map = new HashMap();
        map.put("offset", offset);
        map.put("limit", limit);
        return rillnessService.selectAll(map, rIllness);
    }

    /***
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteIll{id}")
    private int deleteIll(@PathVariable("id") int id) {
        return rillnessService.deleteByPrimaryKey(id);
    }

    /***
     * 添加
     * @param illNess
     * @return
     */
    @PostMapping("addIllness")
    private int addIllness(@RequestBody RIllness illNess) {
        rillnessService.insertSelective(illNess);
        int id = illNess.getId();
        if (id < 10) {
            illNess.setIllCoding("JB00" + illNess.getId());
            return rillnessService.updateByPrimaryKeySelective(illNess);
        } else if (id >= 10) {
            illNess.setIllCoding("JB0" + illNess.getId());
            return rillnessService.updateByPrimaryKeySelective(illNess);
        } else {
            illNess.setIllCoding("JB" + illNess.getId());
            return rillnessService.updateByPrimaryKeySelective(illNess);
        }
    }

    /***
     * 修改前查询
     * @param id
     * @return
     */
    @PostMapping("illBeforeById{id}")
    private RIllness illBeforeById(@PathVariable("id") int id) {
        return rillnessService.beforeUpdateById(id);
    }

    /***
     * 修改
     * @param illness
     * @return
     */
    @PutMapping("illnessUpdate")
    private int illnessUpdate(@RequestBody RIllness illness) {
        return rillnessService.updateByPrimaryKeySelective(illness);
    }
}
